package com.chuanyang.driverh.amap;

import android.content.Context;

import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.MyLocationStyle;
import com.amap.api.services.core.PoiItem;
import com.chuanyang.driverh.R;

/**
 * 地图的工具类
 * @author joyskim
 *
 */
public class AMapUtil {
	private static AMapLocationClient locationClient = null;
	private static AMapLocationClientOption locationOption = null;
	public static void startLocation(Context context, AMapLocationListener listener, long interval) {



		locationClient = new AMapLocationClient(context);
		locationOption = new AMapLocationClientOption();
		// 设置定位模式为高精度模式
		locationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
		//				locationOption.setLocationMode(AMapLocationMode.Device_Sensors);
		//gps定位优先，gps定位才可以获得速度。gps定位才能导航。
//		locationOption.setGpsFirst(true);
		// 设置定位监听
		locationClient.setLocationListener(listener);
		//是否单次定位
		locationOption.setOnceLocation(false);
		//是否解码地址
		locationOption.setNeedAddress(true);
		// 设置发送定位请求的时间间隔,最小值为2000，如果小于2000，按照2000算
		locationOption.setInterval(interval);
		// 设置定位参数
		locationClient.setLocationOption(locationOption);
		locationClient.startLocation();
	}
	public static void stopLocation(){
		if (locationClient!=null){
			locationClient.stopLocation();
		}
		locationOption=null;
		locationClient=null;
	}





//开启一次定位
	public static void startOnceLocation(Context context, AMapLocationListener listener) {
		locationClient = new AMapLocationClient(context);
		locationOption = new AMapLocationClientOption();
		// 设置定位模式为高精度模式
		locationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
		locationClient.setLocationListener(listener);
		locationOption.setInterval(1000);
		//是否单次定位
		locationOption.setOnceLocation(true);
		//是否解码地址
		locationOption.setNeedAddress(true);
		// 设置定位参数
		locationClient.setLocationOption(locationOption);
		locationClient.startLocation();
	}







	/**
	 * 移动到指定的点 并设置指定的zoom
	 * @param latLng
	 * @param zoom
	 */
	public static  void moveToLatLng(AMap amap, LatLng latLng, float zoom){
		if(latLng==null)return;
		amap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, zoom));
	}
	/**
	 * PoiItem-->LocationEntity
	 * @param item
	 * @return
	 */
	public static LocationEntity poiItemToLocationEntity(Context context,PoiItem item){
		LocationEntity locationEntity=new LocationEntity();
		locationEntity.setProvince(item.getProvinceName());
		locationEntity.setCity(item.getCityName());
		locationEntity.setDistrict(item.getAdName());
		locationEntity.setBuilding(item.getTitle());
		locationEntity.setAddress(item.getSnippet());
		locationEntity.setLongitude(item.getLatLonPoint().getLongitude());
		locationEntity.setLatitude(item.getLatLonPoint().getLatitude());
		return locationEntity;
	}

	/**
	 * 设置定位显示的蓝点的显示
	 */
	public static  void setMyLocationStyle(AMap amap){
		MyLocationStyle myLocationStyle = new MyLocationStyle();
//		myLocationStyle.myLocationIcon(BitmapDescriptorFactory.fromResource(R.drawable.ic_location_marker));  //设置我的位置的图标
		myLocationStyle.myLocationIcon(BitmapDescriptorFactory.fromResource(R.mipmap.location_marker));  //设置我的位置的图标
		myLocationStyle.strokeColor(0x00000000);              // 设置圆形的边框颜色
		myLocationStyle.radiusFillColor(0x00000000);     // 设置圆形的填充颜色
		// myLocationStyle.anchor(int,int)    //设置小蓝点的锚点
		myLocationStyle.strokeWidth(0);      // 设置圆形的边框粗细
		amap.setMyLocationStyle(myLocationStyle);  //关于定位的我的位置的显示设置

	}
}
